<template>
  <div class="content">
    <div class="content__left">
      <ul class="category">
        <li
          :class="{
            category__item: true,
            'category__item--active': currentTab === item.tab,
          }"
          v-for="item in categories"
          :key="item.name"
          @click="() => handleTabClick(item.tab)"
        >
          {{ item.name }}
        </li>
      </ul>
    </div>
    <div class="content__right">
      <ul class="product">
        <li class="product__item" v-for="item in list" :key="item._id">
          <img :src="item.imgUrl" alt="" class="product__item__img" />
          <div class="product__item__details">
            <h4 class="product__item__name">{{ item.name }}</h4>
            <p class="product__item__sale">月售{{ item.sales }}件</p>
            <p class="product__item__price">
              <span class="product__item__yen"
                >&yen;<i>{{ item.price }}</i></span
              >
              <span class="product__item__origin"
                >&yen;{{ item.oldPrice }}</span
              >
            </p>
            <div class="product__number">
              <span
                class="product__number__reduce iconfont"
                @click="
                  () => {
                    changeCartItem(shopId, item._id, item, -1, shopName);
                  }
                "
                >&#xeb41;</span
              >
              <span class="product__number__num">
                {{ getProductCartCount(shopId, item._id) }}
              </span>
              <span
                class="product__number__increase iconfont"
                @click="
                  () => {
                    changeCartItem(shopId, item._id, item, 1, shopName);
                  }
                "
                >&#xe627;</span
              >
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { reactive, ref, toRefs, watchEffect } from "vue";
import { useRoute } from "vue-router";
import { useStore } from "vuex";
import { get } from "../../utils/request";
import { usecommonCartEffect } from "../../effects/cartEffects";
const categories = [
  { name: "全部商品", tab: "all" },
  { name: "秒杀", tab: "seckill" },
  { name: "新鲜水果", tab: "fruit" },
];

//Tab切换相关的逻辑
const useTabEffect = () => {
  const currentTab = ref(categories[0].tab);
  const handleTabClick = (tab) => {
    currentTab.value = tab;
  };
  return { currentTab, handleTabClick };
};

//列表内容相关的逻辑
const useCurrentListEffect = (currentTab, shopId) => {
  const content = reactive({ list: [] });
  //获取列表数据内容的逻辑
  const getContentData = async () => {
    const result = await get(`/api/shop/${shopId}/products`, {
      tab: currentTab.value,
    });
    if (result?.errno === 0 && result?.data?.length) {
      content.list = result.data;
    }
  };
  watchEffect(() => {
    getContentData();
  });
  const { list } = toRefs(content);
  return { list };
};
//购物车相关逻辑
const useCartEffect = () => {
  const store = useStore();
  const { changeCartItemInfo, cartList } = usecommonCartEffect();
  const changeShopName = (shopId, shopName) => {
    store.commit("changeShopName", { shopId, shopName });
  };
  const changeCartItem = (shopId, productId, item, num, shopName) => {
    changeCartItemInfo(shopId, productId, item, num);
    changeShopName(shopId, shopName);
  };
  const getProductCartCount = (shopId, productId) => {
    return cartList?.[shopId]?.productList?.[productId]?.count || 0;
  };
  return { cartList, changeCartItem, getProductCartCount };
};

export default {
  name: "Content",
  props: ["shopName"],
  setup() {
    const route = useRoute();
    const shopId = route.params.id;
    const { currentTab, handleTabClick } = useTabEffect();
    const { list } = useCurrentListEffect(currentTab, shopId);
    const { cartList, changeCartItem, getProductCartCount } = useCartEffect();
    return {
      categories,
      currentTab,
      handleTabClick,
      list,
      shopId,
      cartList,
      changeCartItem,
      getProductCartCount,
    };
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/viriables.scss";
@import "../../style/mixins.scss";
.content {
  display: flex;
  position: absolute;
  width: 100%;
  top: 1.54rem;
  left: 0;
  right: 0;
  bottom: 0.5rem;
  &__left {
    width: 20.2667%;
  }
  &__right {
    width: 79.7333%;
  }
}
.category {
  height: 100%;
  overflow-y: scroll;
  background-color: $content-bgcolor;
  &__item {
    font-size: 0.14rem;
    color: $content-fontcolor;
    line-height: 0.4rem;
    text-align: center;
    &--active {
      background-color: $content-fzcolor;
    }
  }
}
.product {
  height: 100%;
  padding: 0 0.16rem;
  overflow-y: scroll;
  &__item {
    display: flex;
    padding: 0.12rem 0;
    border-bottom: 0.01rem solid $content-gbcolor;
    &:last-child {
      border-bottom: none;
    }
    &__img {
      width: 25.47%;
      margin-right: 0.16rem;
    }
    &__details {
      flex: 1;
      display: flex;
      flex-direction: column;
      position: relative;
      overflow: hidden;
      .product__number {
        position: absolute;
        right: 0;
        bottom: 0;
        &__reduce {
          position: relative;
          top: 0.02rem;
          font-size: 0.2rem;
          color: $content-numcolor;
        }
        &__increase {
          position: relative;
          top: 0.02rem;
          font-size: 0.2rem;
          color: $cart-selectedcolor;
        }
        &__num {
          font-size: 0.14rem;
          color: $content-fontcolor;
          line-height: 0.16rem;
          margin: 0 0.1rem;
        }
      }
    }
  }
}
.product__item__name {
  flex: 1;
  font-size: 0.14rem;
  color: $content-fontcolor;
  line-height: 0.2rem;
  @include ellipse;
}
.product__item__sale {
  flex: 1;
  font-size: 0.12rem;
  color: $content-fontcolor;
  line-height: 0.16rem;
  margin: 0.06rem 0;
}
.product__item__price {
  flex: 1;
}
.product__item__price span {
  font-size: 0.12rem;
  line-height: 0.2rem;
  transform: scale(-50%, -50%);
  transform-origin: center top;
}
.product__item__yen {
  color: #e93b3b;
  margin-right: 0.06rem;
}
.product__item__yen i {
  font-size: 0.14rem;
}
.product__item__origin {
  color: #999;
  text-decoration: line-through;
}
</style>